﻿using System;
using Tiny.Imaging.Filter.ChannelBlendLibrary;
using Tiny.Imaging.Filter.PixelLibrary;

namespace Tiny.Imaging.Filter.PointFilter
{
    public class SimpleBlendFilter : BaseFilter.BaseFilter
    {
        private readonly int[] _bMapping;
        private readonly int[] _gMapping;
        private readonly int[] _rMapping;

        public SimpleBlendFilter(Rgba rgba, Func<int, int, int> blend = null, int opacity = 100)
            : base(blend, opacity)
        {
            PrePopulate(ref _rMapping, rgba.R);
            PrePopulate(ref _gMapping, rgba.G);
            PrePopulate(ref _bMapping, rgba.B);
        }

        public override int Process(Rgba rgba)
        {
            rgba.R = _rMapping[rgba.R];
            rgba.G = _gMapping[rgba.G];
            rgba.B = _bMapping[rgba.B];

            return rgba.ToInteger();
        }

        private void PrePopulate(ref int[] mapping, int blendColor)
        {
            mapping = new int[256];
            for (int i = 0; i < 256; ++i)
            {
                mapping[i] = Blend(i, blendColor);
            }
        }
    }
}